package com.zhangyongyi.carhome.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.zhangyongyi.carhome.R;
import com.zhangyongyi.carhome.entity.webview.WebViewResult;

import java.io.UnsupportedEncodingException;

/**
 * Created by dllo on 16/1/22.
 */
public class WebViewActivity extends Activity {
    private WebView webView;
    private String url;
    private String WebUrl;
    private RequestQueue queue;

    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        dialog = new ProgressDialog(this);
        dialog.setTitle("加载中...");
        dialog.show();

        setContentView(R.layout.activity_webview);
        webView = (WebView) findViewById(R.id.web_view);

        Intent intent = getIntent();
        String id = String.valueOf(intent.getIntExtra("id",10));
        Log.d("WebViewActivity", id);
        url = "http://cont.app.autohome.com.cn/autov5.0.0/content/news/newsinfo-pm2-i"
                + id + ".json";
        Log.d("WebViewActivity", url);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());


        initNetWork();
    }

    private void initNetWork() {
        queue = Volley.newRequestQueue(this);
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                dialog.dismiss();
                Gson gson = new Gson();
                WebViewResult result = gson.fromJson(response, WebViewResult.class);
                if (result != null) {
                    WebViewResult.ResultEntity resultEntity = result.getResult();
                    if (resultEntity != null) {
//                        setWebView(resultEntity.getUrl());
                        webView.loadUrl(resultEntity.getUrl());
                    }

                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {

            /**防止乱码 */
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }

        };
        queue.add(request); // 将请求加入到请求队列中
    }

    private void setWebView(String url) {
        Intent intent = getIntent();
        // 初始化
        webView = (WebView) findViewById(R.id.web_view);

        Log.i("url", url);


        // 返回一个设置对象
        WebSettings settings = webView.getSettings();
        // 设置是否能识别js语言
        settings.setJavaScriptEnabled(true);

        // 设置是否支持缩放
        settings.setSupportZoom(true);

        // 设置浏览器引擎
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                Log.i("lanou", "当前网页加载进度" + newProgress);
            }

            ;
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                super.onProgressChanged(view, newProgress);
                Log.i("lanou", "当前网页加载进度:" + newProgress);
            }
        });
        //监听接收到错误信息的回调
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                // TODO Auto-generated method stub
                super.onReceivedError(view, errorCode, description, failingUrl);
                Log.i("lanou", errorCode + "" + description);
            }
        });

        webView.loadUrl(url);
    }

}
